package space.sakana.edusysback.service;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import space.sakana.edusysback.entity.Admin;
import space.sakana.edusysback.entity.Subject;
import space.sakana.edusysback.entity.Teacher;
import space.sakana.edusysback.entity.User;
import space.sakana.edusysback.entity.cls.Cls;
import space.sakana.edusysback.entity.cls.ClsSubject;
import space.sakana.edusysback.entity.cls.ClsTeacher;
import space.sakana.edusysback.entity.cls.ClsUser;
import space.sakana.edusysback.mapper.AdminMapper;
import space.sakana.edusysback.mapper.SubjectMapper;
import space.sakana.edusysback.mapper.TeacherMapper;
import space.sakana.edusysback.mapper.UserMapper;
import space.sakana.edusysback.mapper.cls.ClsMapper;
import space.sakana.edusysback.mapper.cls.ClsSubjectMapper;
import space.sakana.edusysback.mapper.cls.ClsTeacherMapper;
import space.sakana.edusysback.mapper.cls.ClsUserMapper;
import space.sakana.edusysback.result.DataResult;
import space.sakana.edusysback.result.Result;
import space.sakana.edusysback.util.CookieUtil;

import java.util.List;

@Service
public class ClsService {

    private UserMapper userMapper;
    private TeacherMapper teacherMapper;
    private SubjectMapper subjectMapper;
    private ClsMapper clsMapper;
    private ClsUserMapper clsUserMapper;
    private ClsTeacherMapper clsTeacherMapper;
    private ClsSubjectMapper clsSubjectMapper;
    private AdminMapper adminMapper;

    @Autowired
    public void autowiredMapper(UserMapper userMapper, TeacherMapper teacherMapper, SubjectMapper subjectMapper,
    ClsMapper clsMapper, ClsUserMapper clsUserMapper, ClsTeacherMapper clsTeacherMapper, ClsSubjectMapper clsSubjectMapper,
                                AdminMapper adminMapper){
        this.userMapper = userMapper;
        this.teacherMapper = teacherMapper;
        this.subjectMapper = subjectMapper;
        this.clsMapper = clsMapper;
        this.clsUserMapper = clsUserMapper;
        this.clsTeacherMapper = clsTeacherMapper;
        this.clsSubjectMapper = clsSubjectMapper;
        this.adminMapper = adminMapper;
    }

    public Result getCls(int cid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new DataResult(JSON.toJSONString(clsMapper.selectClsById(cid)));
    }

    public Result getClassUsers(int cid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        List<ClsUser> clsUsers = clsUserMapper.selectClsUsersByCid(cid);
        int size = clsUsers.size();
        User[] users = new User[size];
        for (int i = 0; i < size; i++) {
            users[i] = userMapper.selectUserById(clsUsers.get(i).getUid());
        }
        return new DataResult(JSON.toJSONString(users));
    }

    public Result getUserClasses(int uid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        List<ClsUser> clsUsers = clsUserMapper.selectClsUsersByUid(uid);
        int size = clsUsers.size();
        Cls[] clsArray = new Cls[size];
        for (int i = 0; i < size; i++) {
            clsArray[i] = clsMapper.selectClsById(clsUsers.get(i).getCid());
        }
        return new DataResult(JSON.toJSONString(clsArray));
    }

    public Result addClassUser(int uid, int cid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        User user = userMapper.selectUserById(uid);
        if (user==null){
            return Result.failResult;
        }
        Cls cls = clsMapper.selectClsById(cid);
        if (cls==null){
            return Result.failResult;
        }
        ClsUser clsUser = clsUserMapper.selectClsUsersByUidAndCid(uid, cid);
        if (clsUser!=null){
            return Result.failResult;
        }
        clsUser = new ClsUser(uid, cid);
        return new Result(clsUserMapper.insertClsUser(clsUser));
    }

    public Result getClassTeachers(int cid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        List<ClsTeacher> clsTeachers = clsTeacherMapper.selectClsTeachersByCid(cid);
        int size = clsTeachers.size();
        Teacher[] teachers = new Teacher[size];
        for (int i = 0; i < size; i++) {
            teachers[i] = teacherMapper.selectTeacherById(clsTeachers.get(i).getTid());
        }
        return new DataResult(JSON.toJSONString(teachers));
    }

    public Result getTeacherClasses(int tid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        List<ClsTeacher> clsTeachers = clsTeacherMapper.selectClsTeachersByTid(tid);
        int size = clsTeachers.size();
        Cls[] clsArray = new Cls[size];
        for (int i = 0; i < size; i++) {
            clsArray[i] = clsMapper.selectClsById(clsTeachers.get(i).getCid());
        }
        return new DataResult(JSON.toJSONString(clsArray));
    }

    public Result addClassTeacher(String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        ClsTeacher clsTeacher = JSON.parseObject(data, ClsTeacher.class);
        if (clsTeacher!=null){
            return new Result(clsTeacherMapper.insertClsTeacher(clsTeacher));
        }
        return Result.failResult;
    }

    public Result getClassSubjects(int cid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        List<ClsSubject> clsSubjects = clsSubjectMapper.selectClsSubjectsByCid(cid);
        int size = clsSubjects.size();
        Subject[] subjects = new Subject[size];
        for (int i = 0; i < size; i++) {
            subjects[i] = subjectMapper.selectSubjectById(clsSubjects.get(i).getSid());
        }
        return new DataResult(JSON.toJSONString(subjects));
    }

    public Result addClassSubject(String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        ClsSubject clsSubject = JSON.parseObject(data, ClsSubject.class);
        if (clsSubject!=null){
            return new Result(clsSubjectMapper.insertClsSubject(clsSubject));
        }
        return Result.failResult;
    }

    public Result addCls(String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        Cls cls = JSON.parseObject(data, Cls.class);
        if (cls!=null){
            return new Result(clsMapper.insertCls(cls));
        }
        return Result.failResult;
    }

    public Result updateCls(String data, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        Cls cls = JSON.parseObject(data, Cls.class);
        if (cls!=null){
            return new Result(clsMapper.updateCls(cls));
        }
        return Result.failResult;
    }

    public Result getAllClass(HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new DataResult(JSON.toJSONString(clsMapper.selectAllCls()));
    }

    public Result deleteCls(int cid, HttpServletRequest request, HttpServletResponse response) {
        Admin admin = adminMapper.selectAdminByToken(CookieUtil.getCookieValueByKey("token", request));
        if (admin==null){
            return Result.failResult;
        }

        return new Result(clsMapper.deleteClsById(cid));
    }
}
